package com.huiyujinrong.qiangdanbao.activity.my;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.FaceEngineBean;
import com.huiyujinrong.qiangdanbao.bean.FaceFindBean;
import com.huiyujinrong.qiangdanbao.bean.UserInforBean;
import com.huiyujinrong.qiangdanbao.bean.UserUploadBean;
import com.huiyujinrong.qiangdanbao.model.UserNoInforModel;
import com.huiyujinrong.qiangdanbao.network.Api;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class UserNoInforPresenter implements UserNoInforContract.Presenter {

    @Inject
    UserNoInforModel model;

    private UserNoInforContract.View view;
    private Context context;

    public UserNoInforPresenter(UserNoInforContract.View v, Context context) {
        this.view = v;
        this.context = context;
        setupDagger2();
    }

    @Override
    public void getUserInfor(String user_id) {
        model.getUserInforDta(user_id);
    }

    private void setupDagger2() {
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }


    @Override
    public void uploadUserInformation(String user_id, String true_name, String idcard_no,
                                      String organize_name, String ad_code, String ad_path,
                                      File id_just, File id_back, File work_gongpai,
                                      File work_mingpian) {
        if (TextUtils.isEmpty(true_name)) {
            view.showToast("请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(idcard_no)) {
            view.showToast("请输入身份证号");
            return;
        }
        if (TextUtils.isEmpty(organize_name)) {
            view.showToast("请输入所属机构名称");
            return;
        }
        if (TextUtils.isEmpty(ad_path)) {
            view.showToast("请选择工作城市");
            return;
        }
        if (ad_path.contains("全部")) {
            view.showToast("请选择正确的工作城市");
            return;
        }
        if (id_just == null || id_back == null) {
            view.showToast("请上传身份证照");
            return;
        }
        if (work_gongpai == null && work_mingpian == null) {
            view.showToast("请上传工作证明");
            return;
        }
        view.showDialog();
        model.uploadUserInformation(user_id, true_name, idcard_no, organize_name, ad_code, ad_path, id_just, id_back, work_gongpai, work_mingpian);

    }

    /**
     * 查询人脸是否识别
     *
     * @param user_id
     */
    @Override
    public void findZmxyFaceRecordByUserId(String user_id) {
        model.findZmxyFaceRecordByUserId(user_id);
    }


    /**
     * 去人脸识别
     *
     * @param user_id
     */
    @Override
    public void getZmxyFaceEngine(String user_id) {
        view.showDialog();
        model.getZmxyFaceEngine(user_id);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getZmxyFaceEngines(RespEvent<FaceEngineBean> event) {
        if (event.getUrl().equals(Api.getZmxyFaceEngine)) {
            view.dissmissDialog();
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    view.onFaceEngineResult(event.getT().getZmxy_face_url());
                    break;
                case RespEvent.Code.ERROR:
                    view.showToast(event.getExtra().toString());
                    break;
                case RespEvent.Code.FAILURE:
                    view.showToast(event.getExtra().toString());
                    break;
            }

        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void findZmxyFaceRecord(RespEvent<FaceFindBean> event) {
        if (event.getUrl().equals(Api.findZmxyFaceRecordByUserId)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    Log.i("findZmxyFaceRecord", "findZmxyFaceRecord: " + event.getT().getFace_flag());
                    view.findZmxyFaceRecord(event.getT());
                    break;
                case RespEvent.Code.ERROR:
                    break;
                case RespEvent.Code.FAILURE:
                    break;
            }

        }
    }


    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEventUpload(RespEvent<UserUploadBean> event) {
        if (event.getUrl().equals(Api.uploadUinfor)) {
            view.dissmissDialog();
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.showToast("提交成功");
                    view.exit(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("信息提交失败,稍后重试");
                    break;
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEvent(RespEvent<UserInforBean> event) {
        if (event.getUrl().equals(Api.selectUinfor)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.getUserInforData(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }
}
